package acs.offer.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import acs.offer.collection.Params;
import acs.offer.constant.Constant;
import acs.offer.constant.EventType;
import acs.offer.controller.EventBus;
import acs.offer.pojo.TranstractDate;

/**
 * ���ݿ⹤��
 * @author Administrator
 *
 */
public class DbUtil {
	private static DbUtil instance =new DbUtil();
	public static DbUtil getInstance() {
		return instance;
	}

	String key="xiao";
	public Map<String, Object> loadOffer(Params params, TranstractDate transtract){
		Map<String, Object> offers = ServerCache.getInstance().get(transtract.getRequestEnum().toString());
		if(offers == null) {
			offers = loadOffer(params.getOrDefault("duration",1),params.getOrDefault("timeUnit",TimeUnit.HOURS));
		}		
		ServerCache.getInstance().put(transtract.getRequestEnum().toString(),offers);//加入缓存
		
		DMSSoftTimer.getInstance().startTimer(1*2000, EventType.TIMEOUT_FOR_CACHE_OFFER,params);//开始清除缓存倒计时
		
		params.put(Constant.TRANSFER_OFFER, offers);
	    EventBus.getInstance().putMessage(EventType.RESPONSE_FOR_DATA, params);
	    System.out.println("服务端查到了offer，现在开始给客户端返回offer信息");
		return offers;
	}
	private Map<String, Object> loadOffer(int duration,TimeUnit timeUnit){
		try {
			Thread.sleep(2*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		switch (timeUnit) {
			case HOURS:
				System.out.println("查询 "+duration+" 小时前的数据");
				break;
			case MINUTES:
				System.out.println("查询 "+duration+" 分前的数据");
				break;
			case SECONDS:
				System.out.println("查询 "+duration+" 秒前的数据");
				break;
			default:
				System.out.println("查询 "+duration+" 天前的数据");
				break;
		}
		Map<String, Object> result=new HashMap<String, Object>();
		result.put(Constant.CACHE_KEY, "小苏,"+timeUnit);
		return result;

	}

}
